package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
    private final AliOssUtil aliOssUtil;
    @Value("${filePath}")
    private String filePath;
    public CommonController(AliOssUtil aliOssUtil) {
        this.aliOssUtil = aliOssUtil;
    }

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传，{}",file);

//        try {
//            //file.transferTo(new File("D:\\upload\\"+file.getOriginalFilename()));
//            //原始文件
//            String originalFilename = file.getOriginalFilename();
//            //截取文件后缀
//            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//            //使用UUID重新生成文件名，防止文件名称重复造成文件覆盖
//            String fileName = UUID.randomUUID().toString()+substring;
//
//            //文件的请求路径
////            String filePath = aliOssUtil.upload(file.getBytes(), fileName);
//
//            return Result.success(filePath);
//        } catch (Exception e) {
//            log.error("文件上传失败",e);
//        }
//        return Result.error(MessageConstant.UPLOAD_FAILED);
//    }
        String prefix = "";
        //保存上传
        OutputStream out = null;
        InputStream fileInput = null;
        try {
            if (file != null) {
                String originalName = file.getOriginalFilename();
                prefix = originalName.substring(originalName.lastIndexOf(".") + 1);
                String uuid = UUID.randomUUID() + "";
                String filepath = "http://localhost:8080/uploads/" + uuid + "." + prefix;
                String filepath1 = filePath + uuid + "." + prefix;

                File files = new File(filepath1);
                if (!files.getParentFile().exists()) {
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                //打印查看上传路径
                System.out.println(filepath);
                return Result.success(filepath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (fileInput != null) {
                    fileInput.close();
                }
            } catch (IOException e) {
            }
        }
        return Result.success();
    }
}
